package com.nwcl.basic.service.impl;

import com.nwcl.basic.bo.UserBO;
import com.nwcl.basic.dao.UserDao;
import com.nwcl.basic.entity.User;
import com.nwcl.basic.service.UserService;
import com.nwcl.common.constant.Constant;
import com.nwcl.common.utils.BeanUtil;
import com.nwcl.basic.service.bo.YsxRegisterUserBO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author liguiqin
 * @date 2023/5/22
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;

    @Override
    public UserBO getUserByUnionId(String unionId) {
        User user = userDao.getUserByUnionId(unionId);
        if (user == null) {
            return null;
        }
        return BeanUtil.copy(user, UserBO.class);
    }

    @Override
    public UserBO findByPhone(String phone) {
        User user = userDao.findByPhone(phone);
        if (user == null) {
            return null;
        }
       return BeanUtil.copy(user, UserBO.class);
    }


    @Override
    public void updateYsxById(Integer id, String wxUnionId, String headImgUrl, String ysxOpenId, String username) {
        userDao.updateYsxById(id, wxUnionId, headImgUrl, ysxOpenId, username);
    }

    @Override
    public void updateUnionIdById(Integer id, String wxUnionId) {
        userDao.updateUnionIdById(id, wxUnionId);
    }

    @Override
    public void updateHeadImgUrl(Integer id, String headImgUrl) {
        userDao.updateHeadImgUrl(id, headImgUrl);
    }

    @Override
    public void updateYsxOpenId(Integer id, String openId) {
        userDao.updateYsxOpenId(id, openId);
    }

    @Override
    public UserBO saveYsxRegisterUserBO(YsxRegisterUserBO regUser) {
        User user = new User();
        user.setCreateTime(new Date());
        user.setUpdateTime(new Date());
        user.setIsValid(Constant.isValid.valid);
        user.setUsername(regUser.getUsername());
        user.setHeadImgUrl(regUser.getHeadImgUrl());
        user.setYsxOpenId(regUser.getYsxOpenId());
        user.setWxUnionId(regUser.getWxUnionId());
        userDao.save(user);
        return BeanUtil.copy(user, UserBO.class);
    }

    @Override
    public void updatePhoneById(Integer id, String phone) {
        userDao.updatePhoneById(id, phone);
    }

    @Override
    public UserBO getById(Integer id) {
        User user =userDao.getById(id);
        if (user != null) {
            return BeanUtil.copy(user, UserBO.class);
        }
        return null;
    }

    @Override
    public Map<Integer, UserBO> mapByIds(List<Integer> userIds) {
        Map<Integer, UserBO> userMap=new HashMap<>();
        List<User> users= userDao.listByIds(userIds);
        for (User user : users) {
            userMap.put(user.getId(),BeanUtil.copy(user, UserBO.class));
        }
        return userMap;
    }

    @Override
    public void updateUser(UserBO userBO) {
        User user = BeanUtil.copy(userBO, User.class);
        userDao.updateUser(user);
    }
}
